package com.smart.car.member.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.smart.car.common.res.exception.BusinessException;
import com.smart.car.member.entity.MemberVehicle;
import com.smart.car.member.entity.MemberVehicleExample;
import com.smart.car.member.mapper.MemberVehicleMapper;
import com.smart.car.member.service.MemberVehicleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description: TODO
 * @author: zoutao
 * @date: 2021/4/15
 */
@Service
public class MemberVehicleServiceImpl implements MemberVehicleService {

    @Resource
    private MemberVehicleMapper vehicleMapper;

    @Override
    public List<MemberVehicle> list() {
        MemberVehicleExample carExample = new MemberVehicleExample();
        List<MemberVehicle> vehicles = vehicleMapper.selectByExample(carExample);
        return vehicles;
    }

    @Override
    public int addVehicle(String json) throws BusinessException {
        MemberVehicle vehicle = JSONObject.parseObject(json, MemberVehicle.class);
        return vehicleMapper.insertSelective(vehicle);
    }

    @Override
    public MemberVehicle getVehicle(String memberId) throws BusinessException {
        MemberVehicleExample example = new MemberVehicleExample();
        //vehicleNo就是memberId
        example.createCriteria().andMemberIdEqualTo(memberId);
        List<MemberVehicle> vehicles = vehicleMapper.selectByExample(example);
        MemberVehicle vehicle = null;
        if (!ObjectUtil.isEmpty(vehicles)) {
            vehicle = vehicles.get(0);
        }
        return vehicle;
    }
}